package finalfinancialanalyst.business;

import java.io.File;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;


public class CurrencyList implements Serializable  {
    
        private static final long serialVersionUID = 1L;
 
	private ArrayList<Currency> currencies;
        private static CurrencyList instance;
	 
	private CurrencyList() {
            currencies = new ArrayList<Currency>(); 
	 
	}
        
        public static CurrencyList getInstance() {
            if (instance == null) {
                
                //Try to load from file
                instance = (CurrencyList)IO.loadFromFile(IO.getCurrenciesFile());
                if (instance == null) {
                   instance = new CurrencyList();
                   
                   //TODO default data
                   instance.addCurrency(new Currency("Euro", BigDecimal.valueOf(Double.parseDouble("1.3334")), "EUR"));
                   instance.addCurrency(new Currency("U.S. Dollar", BigDecimal.valueOf(Double.parseDouble("1")), "USD"));
                   instance.addCurrency(new Currency("Pound sterling", BigDecimal.valueOf(Double.parseDouble("1.3334")), "GBP"));
                   instance.addCurrency(new Currency("Swiss franc", BigDecimal.valueOf(Double.parseDouble("1.08696")), "CHF"));
                   instance.addCurrency(new Currency("Czech koruna", BigDecimal.valueOf(Double.parseDouble("0.051789")), "CZK"));
                }
            }
            return instance;
        }
        
        public int size() {
            return currencies.size();
        }
        
        public Currency get(int rowIndex) {
            return currencies.get(rowIndex);
        }
	 
	public ArrayList getList() {
		return currencies;
	}
	 
	public boolean updateCurrencyRates() {
		return false;
	}
	 
	public boolean addCurrency(Currency curr) {
                return(currencies.add(curr));
                
	}
	 
	public Currency retrieveCurrency(String toString) {
		for(Currency a:currencies) {
                    if(a.toString().equalsIgnoreCase(toString)) {
                       return a; 
                    }
                }   
                return null;
	}
        
        public Currency retrieveCurrencyBySymbol(String symbol) {
		for(Currency a:currencies) {
                    if(a.getSymbol().equalsIgnoreCase(symbol)) {
                       return a; 
                    }
                }   
                return null;
	}
	 
	public boolean deleteCurrency(Currency curr) {
		return(currencies.remove(curr));
	}
	 
	public boolean saveCSV(File file) {
		return false;
	}
	 
	public boolean loadCSV(File file) {
		return false;
	}
	 
}
 
